{include file="public/layout" /}

<body>
<link rel="stylesheet" href="__SKIN__/css/page.css" media="all">

<div class="layui-fluid" id="LAY-component-layer-list">
    <div class="layui-card">
        <div class="head-oper">
            <div class="fl">
                {if condition="$Request.param.pid > 0"}
                <a href="{:url('Region/index', array('pid'=>$parentInfo.parent_id))}" class="layui-btn layui-btn-primary  input-btn-back"><i class="layui-icon layui-icon-left"></i></a>
                {/if}
                {eq name="$Think.const.CONTROLLER_NAME.'@add'|is_check_access" value="1"}
                <a data-url="{:url('Region/add', array('pid'=>$Request.param.pid))}" class="layui-btn " data-type="Region_add">新增区域</a>
                {/eq}
            </div>
            <div class="fr ">
                <form action="{:url('Region/index')}" method="get" onsubmit="layer_loading();">
                    <div class="fl mt5" >
                        <div class="layui-input-inline w240">
                            {$searchform.hidden|default=''}
        					<input type="hidden" name="pid" id="pid" value="{$Request.param.pid|default=''}">
                       	    <input type="text" name="keywords" id="keywords" value="{$Request.param.keywords}" placeholder="区域搜索" class="layui-input">
                        </div>
    				</div>
    				<div class="layui-input-inline w50 mt5">
    					<button class="layui-btn input-btn-search" type="submit"><i class="layui-icon layui-icon-search"></i></button>
    				</div>
                </form>
            </div>
        </div>

        <div class="layui-card-body house-table">
            <div class="layui-form layui-border-box layui-table-view" lay-filter="demo" lay-id="admin-arctype" >
                <div class="layui-table-box">
                    <div class="layui-table-body " >
                        <table cellspacing="0" cellpadding="0" border="0" class="layui-table layui-form" style="width: 100%" lay-skin="line">
                            <thead >
                            <tr>
                                <th class="w40 tc"> 
                                    <div class="layui-table-cell w40 tc laytable-cell-checkbox">
                                      <input type="checkbox" lay-filter="checkAll" class="checkAll" lay-skin="primary">
                                      <div class="layui-unselect layui-form-checkbox" lay-skin="primary"> <i class="layui-icon layui-icon-ok"></i></div>
                                    </div>
                                </th>
                                <th class="w60 tc"> <div class="layui-table-cell w60 tc" ><span>ID</span></div></th>
                                <th class="wauto"> <div class="layui-table-cell wauto"><span>区域</span></div></th>
                                <th class="w130 tc"> <div class="layui-table-cell w130 tc"><span>上级区域</span></div></th>
                                <th class="w80 tc"> <div class="layui-table-cell w80 tc"><span>启用</span></div></th>
                                <th class="w80 tc"> <div class="layui-table-cell w80 tc"><span>级别</span></div></th>
                                <th class="w80 tc"> <div class="layui-table-cell w80 tc"><span>排序</span></div></th>
                                <th class="w310 tc"> <div class="layui-table-cell w310 tc"><span>操作</span></div></th>
                                {notempty name="$web_region_domain"}
                                <!--<th class="w310 tc"> <div class="layui-table-cell w310 tc"><span>操作</span></div></th>-->
                                {else /}
                                <!--<th class="w230 tc"> <div class="layui-table-cell w230 tc"><span>操作</span></div></th>-->
                                {/notempty}
                            </tr>
                            </thead>
                            <tbody>
                            {empty name="list"}
                            <tr>
                                <td class="no-data" align="center" axis="col0" colspan="8">
                                    <i class="fa fa-exclamation-circle"></i>没有符合条件的数据
                                </td>
                            </tr>
                            {else /}
                            {foreach name="list" item="vo" key="k" }
                            <tr>
                                <td>
                                   <div class="layui-table-cell w40 tc laytable-cell-checkbox">
                                      <input type="checkbox" name="ids[]" lay-filter="ids" lay-skin="primary" value="{$vo.id}">
                                   </div>
                                </td>
                                <td><div class="layui-table-cell w60 tc"> {$vo.id} </div></td>
                                <td>
                                    <div class="layui-table-cell wauto">
                                        <input type="text" onchange="changeTableVal('region','id','{$vo.id}','name',this);" value="{$vo.name}" class="layui-input"/>
                                    </div>
                                </td>
                                <td>
                                    <div class="layui-table-cell w130 tc">
                                        <a href="{:url('Region/index',array('pid'=>$vo['parent_id']))}">{empty name='$parentInfo.name'}无{else/}{$parentInfo.name}{/empty}</a>
                                    </div>
                                </td>
                                <td>
                                    <div class="layui-table-cell w80 tc">
                                        <input type="checkbox" name="status"  data-id="{$vo.id}" value="{$vo.status}" lay-filter="status" lay-skin="switch" lay-text="是|否" {if condition="$vo.status" value="1"} class="yes" checked{else /}class="no"{/if}>
                                    </div>
                                </td>
                                <td><div class="layui-table-cell w80 tc">  {$vo.level} </div></td>
                                <td>
                                    <div class="layui-table-cell w80 tc">
                                        <input type="text" onchange="changeTableVal('region','id','{$vo.id}','sort_order',this);" class="layui-input" value="{$vo.sort_order}" onkeyup="this.value=this.value.replace(/[^\d]/g,'');" onpaste="this.value=this.value.replace(/[^\d]/g,'');" />
                                    </div>
                                </td>
                                <td>
                                    <div class="layui-table-cell {notempty name='$web_region_domain'}w310{else /}w310{/notempty} tl right-oper nav-more">
                                        {eq name="$Think.const.CONTROLLER_NAME.'@edit'|is_check_access" value="1"}
                                        <a data-url="{:url('Region/edit',array('id'=>$vo['id']))}" class="layui-btn btn-edit customvar_edit" data-type="Region_edit" >
                                            编辑
                                        </a>
                                        {/eq}
                                        <a  href="{:url('Region/index',array('pid'=>$vo['id']))}"  class="layui-btn btn-primary">查看下级</a>
                                        {if condition="($vo.is_default eq 1)"}
                                        <a  class="layui-btn btn-del" href="javascript:;">默认区域</a>
                                        {else /}
                                        <a  class="layui-btn btn-primary" href="javascript:;"   onClick="changeSortOrder('{$vo.id}');">设为默认</a>
                                        {/if}
                                        {notempty name="$web_region_domain"}
                                        {/notempty}
                                        {eq name="$Think.const.CONTROLLER_NAME.'@del'|is_check_access" value="1"}
                                        <a href="javascript:void(0);" class="layui-btn btn-primary" data-url="{:url('Region/del')}" data-id="{$vo.id}" onClick="delfun(this);">删除</a> 
                                        {/eq}
                                    </div>
                                </td>
                            </tr>
                            {/foreach}
                            {/empty}
                            </tbody>
                        </table>
                    </div>
                </div>
                <div class="layui-table-page footer-oper">
                    <input type="checkbox" lay-filter="checkAll" class="checkAll" lay-skin="primary" >
                    {eq name="'Region@del'|is_check_access" value="1"}
                    <a class="layui-btn layui-btn-primary" data-type="batch_del" data-url="{:url('Region/del')}" style="line-height: 34px;">
                    批量删除
                    </a>
                    {/eq}
                </div>
                <!--分页-->
            </div>
            {$pageStr}
        </div>
    </div>
</div>

<script>
    layui.config({
        base: '__SKIN__/' //静态资源所在路径
        ,version: '{$version}'
    }).extend({
        index: 'lib/index' //主入口模块
    }).use(['index', 'table'], function(){
        var $ = layui.$
            ,form = layui.form
            ,table = layui.table;
        /* 触发事件 */
        var active = {
            Region_add:function () {
                var iframes = layer.open({
                    type: 2,
                    title: '新增区域',
                    fixed: true, //不固定
                    shadeClose: false,
                    shade: 0.3,
                    content: $(this).attr('data-url')
                });
                layer.full(iframes);
            }
            ,Region_edit: function(){
                var iframes = layer.open({
                    type: 2,
                    title: '编辑区域',
                    fixed: true, //不固定
                    shadeClose: false,
                    shade: 0.3,
                    content: $(this).attr('data-url')
                });
                layer.full(iframes);
            }
            // 批量删除
            ,batch_del: function(){
                batch_del(this,'ids');
            }
        };
        $('#LAY-component-layer-list .layui-btn').on('click', function(){
            var type = $(this).data('type');
            active[type] && active[type].call(this);
        });

        //开启关闭
        form.on('switch(status)', function(obj){
            var url = "{:url('Region/setStatus')}";
            var id = $(this).attr('data-id');
            var status = 1;
            var self = this;
            if ($(self).val() == 1){
                status = 0;
            }
            $.ajax({
                type: 'POST',
                url: url,
                data: {id:id,status:status,_ajax:1},
                dataType: 'json',
                success: function(res){
                    if (res.code == 1) {
                        layer.msg(res.msg, {icon: 1});
                        $(self).val(status);
                        window.location.reload();
                    } else {
                        layer.msg(res.msg, {icon: 5});
                        if(obj.elem.checked == true){
                            $(self).removeAttr("checked");
                        }else{
                            $(self).val(1);
                            $(self).attr("checked","checked");
                        }
                    }
                    form.render();
                },
                error:function (res) {
                    showErrorAlert();
                }
            });
        });

        //开启关闭
        form.on('switch(is_hot)', function(obj){
            var dataid = $(this).attr('data-id');
            if ($(this).val() == 1){
                $(this).val(0);
            }else{
                $(this).val(1);
            }
            changeTableVal('region','id',dataid,'is_hot',this);
        });

        // 监听复选框全选
        form.on('checkbox(checkAll)', function(data){
            if (data.elem.checked) {
                $('.checkAll').attr('checked', true);
                $('input[name*=ids]').attr('checked', true);
            } else {
                $('.checkAll').attr('checked', false);
                $('input[name*=ids]').attr('checked', false);
            }
            form.render('checkbox');
        });

        // 监听每行复选框
        form.on('checkbox(ids)', function(data){
            if (data.elem.checked && $('input[name*=ids]:checked').length == $('input[name*=ids]').length) {
                $('.checkAll').attr('checked', true);
            } else {
                $('.checkAll').attr('checked', false);
            }
            form.render('checkbox');
        });
        form.render();
    });
    //修改是否默认
    function changeSortOrder(id) {
        var url = "{:url('Region/setSortOrder')}";
        $.ajax({
            type: 'POST',
            url: url,
            data: {id:id,_ajax:1},
            dataType: 'json',
            success: function(res){
                if (res.code == 1) {
                    layer.msg(res.msg, {icon: 1});
                    window.location.reload();
                    if (1 == res.data.refresh) {
                        window.location.reload();
                    }
                } else {
                    layer.msg(res.msg, {icon: 5}, function(){
                        window.location.reload();
                    });
                }
            },
            error:function (res) {
                showErrorAlert();
            }
        });
    }
</script>
{include file="public/footer" /}
